package com.demo.elasticsearch.simple.controller;

import com.demo.elasticsearch.simple.entity.BankEntity;
import com.demo.elasticsearch.simple.entity.PageVO;
import com.demo.elasticsearch.simple.repository.BankRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "测试接口")
@RequestMapping("demo")
@RestController
public class SearchController {
    @Autowired
    private BankRepository bankRepository;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    @ApiOperation(value = "分页查询")
    @PostMapping("page")
    public Page<BankEntity> page(@RequestBody PageVO pageVo) {
        PageRequest page = PageRequest.of(pageVo.getPageNum(), pageVo.getPageSize());
        return this.bankRepository.findAll(page);
    }

    @ApiOperation(value = "关键字查找")
    @PostMapping("keywords")
    public List<BankEntity> keywords(@RequestParam("keyword") String keyWord) {
        CriteriaQuery query = new CriteriaQuery(
                new Criteria("gender").contains(keyWord));
        SearchHits<BankEntity> result = elasticsearchOperations.search(query, BankEntity.class, IndexCoordinates.of("bank"));
        return result.getSearchHits().stream().map(e -> e.getContent()).collect(Collectors.toList());
    }

    @ApiOperation(value = "分组查询")
    @PostMapping("groupByField")
    public List<BankEntity> groupByField(@RequestParam("field") String field) {
        Query groupQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchAllQuery())
                .addAggregation(AggregationBuilders.terms(field).field(field))
                .build();
        SearchHits<BankEntity> result = this.elasticsearchOperations.search(groupQuery, BankEntity.class);
        return result.getSearchHits().stream().map(e -> e.getContent()).collect(Collectors.toList());
    }


    @ApiOperation(value = "分组查询关键字")
    @PostMapping("groupWithQuery")
    public List<BankEntity> groupWithQuery(@RequestParam("field") String field, @RequestParam("gender") String gender) {
        Query groupQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("gender", gender))
                .addAggregation(AggregationBuilders.terms(field).field(field))
                .build();
        SearchHits<BankEntity> result = this.elasticsearchOperations.search(groupQuery, BankEntity.class);
        return result.getSearchHits().stream().map(e -> e.getContent()).collect(Collectors.toList());
    }
}
